from typing import Optional

from tortoise.query_utils import Prefetch

from common.base import Dao
from models.area import Area


class AreaDao(Dao):

    async def all_parent(self, parent: Optional[int] = None):
        _fields = ['id', 'name']
        if parent is None: # true 有数据
            return await self.model.filter(parent=None).all().values(*_fields)
        data = await self.model.filter(id=parent).first().values(*_fields)
        subs = await self.model.filter(parent=parent).all().values(*_fields)
        return {**data, "subs": subs}

    async def get_area(self, **kwargs):
        # 外键关联查询
        return await self.model.filter(parent=330000).all()


area_dao = AreaDao(Area)
